<?php
namespace app\forum\util;
/**
 * UUB转换HTML
 */
class Ubb
{
    
    static public function ubbToHtml($str = '',$config = 'url,image,size,flash,code'){
        $str = trim($str);
        $str = str_replace("\n","<br>",$str);
        $config = explode(',', $config);
        foreach ($config as $value) {
            $name = 'to'.ucfirst($value);
            $str = call_user_func_array(array(self,$name), array($str));
        }
        return $str;
    }

    /**
     * 转URL
     */
    static public function toUrl($str = ''){
        return preg_replace("/\[url\](.+?)\[\/url\]/is","<a href=\"\\1\" target='_blank'>\\1</a>", $str);
    }

    /**
     * 转image
     */
    static public function toImage($str = ''){
        return preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\\1>", $str);
    }

    /**
     * 转字体大小
     */
    static public function toSize($str = ''){
        return preg_replace("/\[size\=(.+?)\](.+?)\[\/size\]/is","<font style=\"font-size: \\1px\">\\2</font>",$str);
    }

    /**
     * 转flash
     */
    static public function toFlash($str = ''){
        return preg_replace("/\[swf\](.+?)\[\/swf\]/is",'<embed src="\\1" quality="high" width="480" height="400" align="middle" allowScriptAccess="always" allowFullScreen="true" mode="transparent" type="application/x-shockwave-flash"></embed>', $str);
    }

    /**
     * 转code
     */
    static public function toCode($str = ''){
        return preg_replace("/\[code\](.+?)\[\/code\]/is",'<pre>\\1</pre>', $str);
    }
}